using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using SolanumSolstice.Units;
using SolanumSolstice.Mapping;


namespace SolanumSolstice.Buildings
{
    public class RoadBlock
    {

        protected Texture2D horizontal;
        protected Texture2D vertical;
        protected Vector2 position;
        protected bool isVertical;
        protected bool isConstructed;
        protected bool isAcceptable;
        protected int woodCost;
        protected bool isDead;
        protected int width;
        protected int height;
        protected int hitPoints;
        protected int maxHitPoints;
        protected bool rightBumperIsPressed;
        protected int damage;
        protected ContentManager content;

        #region Accessors
        public Vector2 Position
        {
            get
            {
                return position;
            }
            set
            {
                position = value;
            }
        }

        public bool IsDead
        {
            get
            {
                return isDead;
            }
            set
            {
                isDead = value;
            }
        }

        public bool IsVertical
        {
            get
            {
                return isVertical;
            }
            set
            {
                isVertical = value;
            }
        }

        public bool IsConstructed
        {
            get
            {
                return isConstructed;
            }
            set
            {
                isConstructed = value;
            }
        }

        public bool IsAcceptable
        {
            get
            {
                return isAcceptable;
            }
            set
            {
                isAcceptable = value;
            }
        }

        public int HitPoints
        {
            get
            {
                return hitPoints;
            }
            set
            {
                hitPoints = value;
            }
        }

        public int MaxHitPoints
        {
            get
            {
                return maxHitPoints;
            }
            set
            {
                maxHitPoints = value;
            }
        }

        public int Height
        {
            get
            {
                return height;
            }
            set
            {
                height = value;
            }
        }

        public int Width
        {
            get
            {
                return width;
            }
            set
            {
                width = value;
            }
        }

        public int WoodCost
        {
            get
            {
                return woodCost;
            }
            set
            {
                woodCost = value;
            }
        }

        public int Damage { get { return damage; } set { damage = value; } }

        #endregion

        public void setContent(string horiz, string vert) {
            horizontal=content.Load<Texture2D>(horiz);
            vertical=content.Load<Texture2D>(vert);
        }


        public RoadBlock(ContentManager cont, Vector2 position)  
        {
            content=cont;
            setContent("Content\\Txtr\\Bldg\\barrier_wooden_horizontal","Content\\Txtr\\Bldg\\barrier_wooden_vertical" );
            this.position = position;
            this.isVertical = false;
            width = 64;
            height = 32;
            isConstructed = false;
            hitPoints = 50;
            maxHitPoints = 50;
            woodCost = 10;
            damage = 0;
        }

        public void Update(GameTime gameTime, GamePadState gamePad, GameCursor gameCursor)
        {
            if (isConstructed == true)
            {
                
            }
            else
            {
                int modX = gameCursor.ScreenPositionX % 32;
                int modY = gameCursor.ScreenPositionY % 32;

                position = Map.GetMapPosition(new Vector2(gameCursor.ScreenPositionX - modX, (int)gameCursor.ScreenPositionY - modY));
                isAcceptable = SolanumSolstice.RoadBlockIsAcceptable(this);
                
                if (gamePad.Buttons.RightShoulder == ButtonState.Pressed)
                {

                    if (rightBumperIsPressed == false)
                    {
                        if (isVertical == false)
                        {
                            isVertical = true;

                            width = 32;
                            height = 64;
                        }
                        else
                        {
                            isVertical = false;

                            width = 64;
                            height = 32;
                        }
                    }
                    rightBumperIsPressed = true;
                }
                else
                {
                    rightBumperIsPressed = false;
                }
            }


            
        }

        public void Draw(SpriteBatch spritebatch, int mapFocusX, int mapFocusY)
        {
            if (isConstructed)
            {

                float hpPercent = (float)hitPoints / (float)maxHitPoints;

                if (isVertical == true)
                {
                    spritebatch.Draw(vertical,
                        new Rectangle((int)position.X - mapFocusX, (int)position.Y - mapFocusY,(int)(width * hpPercent), height),
                         Color.White);
                }
                else
                {
                    spritebatch.Draw(horizontal,
                        new Rectangle((int)position.X - mapFocusX, (int)position.Y - mapFocusY, width, (int)(height * hpPercent)),
                         Color.White);
                }
            }
            else
            {
                Color colorAcceptable = Color.Green;

                if (isAcceptable == false)
                {
                    colorAcceptable = Color.Red;
                }

                if (isVertical == true)
                {
                    spritebatch.Draw(vertical,
                        new Rectangle((int)position.X - mapFocusX, (int)position.Y - mapFocusY, width, height), colorAcceptable);
                }
                else
                {
                    spritebatch.Draw(horizontal,
                        new Rectangle((int)position.X - mapFocusX, (int)position.Y - mapFocusY, width, height), colorAcceptable);
                }

            }
        }

        public void TakeDamage(int amount)
        {
            hitPoints -= amount;

            if (hitPoints <= 0)
            {
                isDead = true;
                hitPoints = 0;
            }
        }

        public void healDamage(int amount)
        {
            hitPoints += amount;

            if (hitPoints > maxHitPoints)
            {
                hitPoints = maxHitPoints;
            }
        }

    }
}


